﻿package media 
{
	
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	import debugs.Debug;
	import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class sound extends Sprite {
        private var url:String = "";
        private var song:SoundChannel;
		private var soundFactory:Sound;
        public function sound(s:String = null) {
			Debug.send(this + ".sound: " + s);
			if (s != null) {
				url = s;
			}
			var request:URLRequest = new URLRequest(url);
			soundFactory = new Sound();
			//soundFactory.addEventListener(Event.COMPLETE, completeHandler);
           // soundFactory.addEventListener(Event.ID3, id3Handler);
           // soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            //soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            soundFactory.load(request);
			song = new SoundChannel;
        }
		public function play(s:Number = 0):void {
			try{
				song = soundFactory.play(s * 1000);
				song.addEventListener(Event.SOUND_COMPLETE, sc);
			}catch (e:TypeError) {
				//Debug.send(this + ".soundURL: " + url);
			}
		}
		public function stop():void {
			song.stop();
			try{
				soundFactory.close();
			}catch (e:Error) {
				Debug.send(this + ".没有流.");
			}
		}
		private function sc(e:Event):void {
			//Debug.send(this + ".sound.com");
			dispatchEvent(e);
		}
        private function completeHandler(event:Event):void {
            //Debug.send("completeHandler: " + event);
        }

        private function id3Handler(event:Event):void {
            //Debug.send("id3Handler: " + event);
        }

        private function ioErrorHandler(event:Event):void {
            //Debug.send("ioErrorHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //Debug.send("progressHandler: " + event);
        }
    }
}